var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    colorArrays: ["#85B8CF", "#90C652", "#D8AA5A", "#FC9F9D", "#0A9A84", "#61BC69", "#12AEF3", "#E29AAD"],
    class_num: '',
    identity: '',
    username: '',
    loading: false
  },

  showCardView: function(e) {
    var that = this
    var id = e.currentTarget.dataset.index
    wx.navigateTo({
      url: '../courseinfo/courseinfo?id=' + id
    })
  },

  courseListRequest: function(identity, username, classnum) {
    var that = this
    var header = app.globalData.header
    switch (identity) {
      case 'teacher':
        {
          wx.request({
            url: 'http://139.199.192.171:8080/nanitao/tea_course_info/list.action',
            method: 'GET',
            data: {
              tea_num: username
            },
            header: header,
            success: function(res) {
              var data = res.data
              that.setData({
                list: data,
                loading: false
              })
              wx.setStorage({
                key: 'infokey',
                data: data,
              })
            }
          })
          break
        }
      case 'student':
        {
          wx.request({
            url: 'http://139.199.192.171:8080/nanitao/course_info/list.action',
            method: 'GET',
            data: {
              class_num: classnum
            },
            header: header,
            success: function(res) {
              var data = res.data
              that.setData({
                list: data,
                loading: false
              })
              wx.setStorage({
                key: 'infokey',
                data: data,
              })
            },
          })
          break
        }
    }
  },

  refresh: function(identity, username, classnum) {
    var that = this
    var header = app.globalData.header
    that.courseListRequest(identity, username, classnum)
    wx.stopPullDownRefresh({
      success: function() {
        console.log("end")
      }
    })
  },


  onPullDownRefresh: function() {
    var that = this
    var identity = app.globalData.identity
    var classnum = app.globalData.classnum
    var username = wx.getStorageSync("username")
    that.refresh(identity, username, classnum)
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this
    that.setData({
      loading: true
    })
    var value = wx.getStorageSync("infokey")
    var identity = app.globalData.identity
    var username = wx.getStorageSync("username")
    var classnum = app.globalData.classnum
    if (!value) {
      var that = this
      that.courseListRequest(identity, username, classnum)
    } else {
      that.setData({
        list: value,
        loading: false
      })
    }

  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },


  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})